package com.hulk.dryad.admin.control;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hulk.dryad.admin.api.vo.RoleVO;
import com.hulk.dryad.admin.api.vo.SysRoleVO;
import com.hulk.dryad.admin.mapstruct.RoleStruct;
import com.hulk.dryad.admin.mapstruct.SysRoleStruct;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.constant.CacheConstants;
import com.hulk.dryad.persistence.entity.RoleModel;
import com.hulk.dryad.persistence.entity.SysRoleModel;
import com.hulk.dryad.persistence.service.SysRoleMenuService;
import com.hulk.dryad.persistence.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author kim
 * @date 2020-02-10
 */
@ApiSort(5)
@Api(value = "role", tags = "角色管理模块")
@RestController
@AllArgsConstructor
@RequestMapping("/admin/role")
public class SysRoleController {

	private final SysRoleService sysRoleService;

	private final SysRoleMenuService sysRoleMenuService;

	/**
	 * 通过ID查询角色信息
	 * @param id ID
	 * @return 角色信息
	 */
	@ApiOperation(value = "getById", notes = "通过ID查询角色信息")
	@PrintlnLog
	@GetMapping("/{id}")
	public R getById(@PathVariable Integer id) {
		return R.ok(sysRoleService.getById(id));
	}

	/**
	 * 添加角色
	 * @param sysRole 角色信息
	 * @return success、false
	 */
	@ApiOperation(value = "saveRole", notes = "添加角色")
	@Idempotent
	@PrintlnLog
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_role_add')")
	@CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
	public R save(@Valid @RequestBody SysRoleVO sysRole) {
		SysRoleModel model =	SysRoleStruct.INSTANCE.to(sysRole);
		return R.ok(sysRoleService.save(model));
	}

	/**
	 * 修改角色
	 * @param sysRole 角色信息
	 * @return success/false
	 */
	@ApiOperation(value = "updateRole", notes = "修改角色")
	@Idempotent
	@PrintlnLog
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_role_edit')")
	@CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
	public R update(@Valid @RequestBody SysRoleVO sysRole) {
		SysRoleModel model =	SysRoleStruct.INSTANCE.to(sysRole);
		return R.ok(sysRoleService.updateById(model));
	}

	/**
	 * 删除角色
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "removeById", notes = "删除角色")
	@Idempotent
	@PrintlnLog
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_role_del')")
	@CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
	public R removeById(@PathVariable Integer id) {
		return R.ok(sysRoleService.removeRoleById(id));
	}

	/**
	 * 获取角色列表
	 * @return 角色列表
	 */
	@ApiOperation(value = "listRoles", notes = "获取角色列表")
	@PrintlnLog
	@GetMapping("/list")
	public R listRoles() {
		return R.ok(sysRoleService.list(Wrappers.emptyWrapper()));
	}

	/**
	 * 分页查询角色信息
	 * @param page 分页对象
	 * @return 分页对象
	 */
	@ApiOperation(value = "getRolePage", notes = "分页查询角色信息")
	@PrintlnLog
	@GetMapping("/page")
	public R getRolePage(Page page) {
		return R.ok(sysRoleService.page(page, Wrappers.emptyWrapper()));
	}

	/**
	 * 更新角色菜单
	 * @param role 角色对象
	 * @return success、false
	 */
	@ApiOperation(value = "saveRoleMenus", notes = "更新角色菜单")
	@Idempotent
	@PrintlnLog
	@PutMapping("/menu")
	@PreAuthorize("@pms.hasPermission('sys_role_perm')")
	public R saveRoleMenus(@RequestBody RoleVO role) {
		RoleModel model = RoleStruct.INSTANCE.to(role);
		return R.ok(sysRoleService.updateRoleMenus(model));
	}

	/**
	 * 通过角色ID 查询角色列表
	 * @param roleIdList 角色ID
	 * @return
	 */
	@ApiOperation(value = "getRoleList", notes = "通过角色ID 查询角色列表")
	@PrintlnLog
	@PostMapping("/getRoleList")
	public R getRoleList(@RequestBody List<Integer> roleIdList) {
		return R.ok(sysRoleService.findRolesByRoleIds(roleIdList, CollUtil.join(roleIdList, StrUtil.UNDERLINE)));
	}

}
